package com.wind.framework.web.core;

import cn.hutool.core.date.TimeInterval;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ClassUtils;

import java.net.InetAddress;

/**
 * @author gaoyiyang
 **/
@Slf4j
public class WindApplication {

    public static ConfigurableApplicationContext run(String module, Class<?> source, String... args) {
        TimeInterval timeInterval = new TimeInterval(true);
        SpringApplicationBuilder builder = createSpringApplicationBuilder(module, source, args);
        ConfigurableApplicationContext applicationContext = builder.run(args);
        int port = 0;
        if (applicationContext instanceof AnnotationConfigServletWebServerApplicationContext) {
            WebServer webServer = ((AnnotationConfigServletWebServerApplicationContext) applicationContext).getWebServer();
            port = webServer.getPort();
        }
        log.info("""
                
                * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                * *                         _ooOoo_                               * *
                * *                        o8888888o                              * *
                * *                        88" . "88                              * *
                * *                        (| ^_^ |)                              * *
                * *                        O\\  =  /O                              * *
                * *                     ____/`---'\\____                           * *
                * *                   .'  \\\\|     |//  `.                         * *
                * *                  /  \\\\|||  :  |||//  \\                        * *
                * *                 /  _||||| -:- |||||-  \\                       * *
                * *                 |   | \\\\\\  -  /// |   |                       * *
                * *                 | \\_|  ''\\---/''  |   |                       * *
                * *                 \\  .-\\__  `-`  ___/-. /                       * *
                * *               ___`. .'  /--.--\\  `. . ___                     * *
                * *             ."" '<  `.___\\_<|>_/___.'  >'"".                  * *
                * *           | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |                 * *
                * *           \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /                 * *
                * *     ========`-.____`-.___\\_____/___.-`____.-'========         * *
                * *                          `=---='                              * *
                * *     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        * *
                * *           佛祖保佑       永不宕机     永无BUG                     * *
                * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                
                服务{}启动成功,启动端口:[{}],启动耗时:{}ms""", module, port, timeInterval.intervalMs());

        String apiAddress = "http://";
        String localAddress = "localhost";
        try {
            localAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            log.error("获取本机Ip错误", e);
        }
        apiAddress = apiAddress + localAddress + ":" + port + "/doc.html";
        log.info("""
                        
                        \t项目启动成功！
                        \t接口文档: \t{}\s""",
                apiAddress);

        return applicationContext;
    }

    public static SpringApplicationBuilder createSpringApplicationBuilder(String module, Class<?> source, String... args) {
        return new SpringApplicationBuilder(source);
    }

}
